import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimeDisplay {

    private JLabel timeLabel;

    private class TimeSchedule extends TimerTask {
        public void run() {
            timeLabel.setText(getTime());
        }
    }

    public static String getTime() {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return format.format(date);
    }

    public TimeDisplay(JLabel timeLabel) {
        this.timeLabel = timeLabel;
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimeSchedule(), 0, 1000);
    }
}
